package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.ReturnMapper;
import cn.supermarket.pojo.dto.ReturnSimpleDTO;
import cn.supermarket.pojo.entity.ReturnGoods;
import cn.supermarket.pojo.vo.ReturnInfoVO;
import cn.supermarket.pojo.vo.ReturnSimpleVO;
import cn.supermarket.service.iservice.IReturnGoodsService;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.State;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ReturnGoodsServiceImpl implements IReturnGoodsService {

    @Autowired
    private ReturnMapper returnMapper;

    @Override
    public void addReturnGoods(ReturnSimpleDTO returnSimpleDTO) {
        ReturnGoods returnGoods = new ReturnGoods();
        BeanUtils.copyProperties(returnSimpleDTO,returnGoods);
        SnowFlakeGenerateIdWorker autoId = new SnowFlakeGenerateIdWorker(2,22);
        long supId = autoId.nextId();
        LocalDateTime now = LocalDateTime.now();
        returnGoods.setId(supId);
        returnGoods.setCreateDate(now);
        returnGoods.setUpdateDate(now);
        returnGoods.setIsEnable(1);
        returnGoods.setIsDisplay(1);
        returnGoods.setIsCancel(0);
        int rows = returnMapper.insertReturn(returnGoods);
        if (rows != 1){
            throw new ServiceException(State.ERR_PRODUCT_INSERT,"插入商品信息错误,请重试!");
        }
    }

    @Override
    public void updateRemark(Long id, String remark) {
        ReturnSimpleVO remarkId = returnMapper.getById(id);
        //判断id是否存在
        if (remarkId == null){
            throw new ServiceException(State.ERR_UNKNOWN_ERROR,"查询id不存在!");
        }
        int rows = returnMapper.updateRemark(id, remark);
        returnMapper.updateDate(id,LocalDateTime.now());
        if (rows == 0){
            throw new ServiceException(State.ERR_UPDATE,"备注修改失败,请重新修改!");
        }
    }

    @Override
    public void updateIsDisplay(Long id, Integer isDisplay) {
        ReturnSimpleVO isDisplayId = returnMapper.getById(id);
        if (isDisplayId == null){
            throw new ServiceException(State.ERR_UNKNOWN_ERROR,"查询id不存在!");
        }
        if (isDisplayId.getIsDisplay().equals(isDisplay)){
            throw new ServiceException(State.ERR_UPDATE,"修改失败,请重新尝试...");
        }
        if (isDisplay == 1 || isDisplay == 0){
            returnMapper.updateIsDisplay(id, isDisplay);
            returnMapper.updateDate(id,LocalDateTime.now());
        }else {
            throw new ServiceException(State.ERR_BAD_REQUEST,"修改参数错误,请重新尝试!");
        }
    }

    @Override
    public void updateIsCancel(Long id, Integer isCancel) {
        ReturnSimpleVO isCancelId = returnMapper.getById(id);
        if (isCancelId == null){
            throw new ServiceException(State.ERR_UNKNOWN_ERROR,"查询id不存在!");
        }
        if (isCancelId.getIsCancel().equals(isCancel)){
            throw new ServiceException(State.ERR_UPDATE,"修改失败,请重新尝试...");
        }
        if (isCancel == 1 || isCancel == 0){
            returnMapper.updateIsCancel(id, isCancel);
            returnMapper.updateDate(id,LocalDateTime.now());
        }else {
            throw new ServiceException(State.ERR_BAD_REQUEST,"修改参数错误,请重新尝试!");
        }
    }

    @Override
    public List<ReturnInfoVO> selectByOrderId(Long orderId) {
        List<ReturnInfoVO> list = returnMapper.getByOrderId(orderId);
        if (list.isEmpty()){
            throw new ServiceException(State.ERR_NOT_FOUND,"此订单号不存在,请重新输入正确单号!");
        }
        return list;
    }

    @Override
    public List<ReturnInfoVO> selectByDate(Date date) {
        List<ReturnInfoVO> list = returnMapper.getByDate(date);
        if (list.isEmpty()){
            throw new ServiceException(State.ERR_NOT_FOUND,"该日期内没有退货信息,请重新查找!");
        }
        return list;
    }

    @Override
    public List<ReturnInfoVO> selectByReturner(String returner) {
        List<ReturnInfoVO> list = returnMapper.getByReturner(returner);
        if (list.isEmpty()){
            throw new ServiceException(State.ERR_NOT_FOUND,"没有该退货信息,请重新输入退货人!");
        }
        return list;
    }
}
